package com.eschool.beans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.eschool.beans.datamodels.ClassDataModel;
import com.eschool.dao.ClassDao;
import com.eschool.model.Clas;
import com.eschool.service.SchoolManagementService;
import com.eschool.service.impl.SchoolManagementServiceImpl;
import com.eschool.util.ConstraintsUtil;

@ManagedBean(name="classStatusUpdateBean")
@ViewScoped
public class ClassStatusUpdateBean  {

	private Clas[] selectedClassesList;
    private ClassDataModel classModel;
	SchoolManagementService service = new SchoolManagementServiceImpl();	
	private static List<Clas> classes;
	
	private boolean newStatus;

	public boolean isNewStatus() {
		return newStatus;
	}

	public void setNewStatus(boolean newStatus) {
		this.newStatus = newStatus;
	}

	public ClassStatusUpdateBean() {
		classes = new ArrayList<Clas>();
		
		//Map  conditions = ConstraintsUtil.getConstraintsMap();
		
		//System.out.println(UserUtil.currentUser().getName() + "   "  + UserUtil.currentUser().getBranch());
		
		//students = StudentDao.getStudentsByBranch(UserUtil.currentUser().getBranch());

		//students = StudentDao.getStudents(null);
		
		classes = ClassDao.getClasses(ConstraintsUtil.getConstraintsMap());		
		classModel = new ClassDataModel(classes);
	}
	
	public Clas[] getSelectedClassesList() {
		return selectedClassesList;
	}

	public void setSelectedClassesList(Clas[] selectedClassesList) {
		this.selectedClassesList = selectedClassesList;
	}

	public ClassDataModel getClassModel() {
		return classModel;
	}

	public void setClassModel(ClassDataModel classModel) {
		this.classModel = classModel;
	}

	public static List<Clas> getClasses() {
		return classes;
	}

	public static void setClasses(List<Clas> classes) {
		ClassStatusUpdateBean.classes = classes;
	}

	public String perform(){
    	
    	List selectedClasses = Arrays.asList(selectedClassesList);

    	service.updateClassesStatus(selectedClasses, newStatus);
    	
    	FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
    					FacesMessage.SEVERITY_INFO,"Info Message", "Classes' Status Updated"));
    	
    	return "classStatusUpdate";
    }
}
